<?php
require_once ("log.php");
error_reporting(E_ERROR);
require_once ("cloud_api.php");
class cloud_my {
	// 进入主界面 
	function my_main() { 
		global $v_file;
		require($v_file);
	}

	// 获取发单群列表
	function get_send_groups() {
		$bot_id =  $_COOKIE['bot_id'];
		$group_list = "";
		if (empty($bot_id)) {
			echo "";
			exit;
		} else {
			$group_data = AllQuery("SELECT * FROM wechat_group WHERE robot_id = {$bot_id}");
			for ($i = 0; $i < count($group_data); ++$i) {
				$row = $group_data[$i];
				$group_list .= "<li data-id = '82400' >";
				$from_name = $row['wechat_room_name'];
				$group_list .= "<input class = 'li-input4' readonly = 'readonly' type = 'text' value = {$from_name}>";

				$start_time = $row['start_time'];
				$group_list .= "<input class = 'li-input' readonly = 'readonly' type = 'text' value = {$start_time}>";

				$finish_time = $row['finish_time'];
				$group_list .= "<input class = 'li-input' readonly = 'readonly' type = 'text' value = {$finish_time}>";

				$interval = $row['interval'];
				$group_list .= "<input class = 'li-input' readonly = 'readonly' type = 'text' value = {$interval}>";

				$status = $row['status'];
				if ($status == "1") {
					$status = "发单";
					$group_list .= "<input class = 'li-input1' readonly = 'readonly' type = 'text' value = {$status}>";
				} else {
					$status = "暂停";
					$group_list .= "<input class = 'li-input' readonly = 'readonly' type = 'text' value = {$status}>";
				}

				$serial = $row['id'];
				$group_list .= "<button class = 'btn_setting' value = {$serial} onclick = send_group_data({$serial}) href = 'javascript:;' >设置</button>";
				$group_list .= "</li>";
			}
		}
		echo $group_list;
	}

	// 获取其中一个发单群
	function get_send_group() {
		$serial = $_POST['serial'];
		$row = simQuery("SELECT * FROM wechat_group WHERE id = {$serial}");
		if (!empty($row)) {
			$g_serial			= $row['id'];					// 唯一码
			$g_from_name	 	= $row['wechat_room_name'];		// 群名
			setcookie("g_serial", $g_serial, time()+1800);
			setcookie("g_from_name", $g_from_name, time()+1800);

			echo "2"; // 获取该群信息成功
		} else {
			echo "1"; // 该群可能已经被删除
		}
	}

	// 离开发单群，回到主界面
	function exit_send_group() {
		setcookie("g_serial", "", time()-1800);
		setcookie("g_from_name", "", time()-1800);
		echo "1"; // 获取该群信息成功
	}

	// 判断是否可以创建新群
	function create_new_group() {
		$bot_id = $_COOKIE['bot_id'];
		if (empty($bot_id)) {
			echo json_encode(array(
				'state' => -1,
				'msg'	=> '登录超时!',
			));
			exit;
		}

		$rob_data = simQuery("SELECT * FROM robot WHERE id = {$bot_id}");
		if (empty($rob_data) || empty($rob_data['wechat_id'])) {
			echo json_encode(array(
				'state' => -2,
				'msg'	=> '当前没有登录微信!',
			));
			exit;
		}

		$addr = simQuery("SELECT * FROM server WHERE id = {$rob_data['server_id']}");
		if (empty($rob_data) || empty($rob_data['wechat_id'])) {
			echo json_encode(array(
				'state' => -3,
				'msg'	=> '没有该服务器!',
			));
			exit;
		}

		$host 				= $addr['host'];
		$port				= $addr['port'];
		$data['id']			= $bot_id;
		$data['fun'] 		= 'ipad_sync_message';
		$data['sign'] 		= generateSign($data);
		$result = json_decode(sendZSHttp($host, $port, '/api/api_accept_post.php', $data), true);
		if ($result['state'] < 0) {
			$result['state'] -= 3;
		}

		echo json_encode($result);
	}
}

/* End of file democontroller.php */